package com.blueskykong.singleton;

/**
 * 懒汉式
 * dcl
 *
 * @author keets
 * @date 2017/11/5
 */
public class DoubleCheckLock {

    private String word;

    private static volatile DoubleCheckLock instance = null;

    private DoubleCheckLock() {
    }

    public static DoubleCheckLock getInstance() {
        if (instance == null) {
            synchronized (DoubleCheckLock.class) {
                if (instance == null) {
                    instance = new DoubleCheckLock();
                }
            }
        }
        return instance;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public static void main(String[] args) {
        DoubleCheckLock d1 = DoubleCheckLock.getInstance();
        d1.setWord("hello");
        DoubleCheckLock d2 = DoubleCheckLock.getInstance();
        d2.setWord("word");

        if (d1 == d2) {
            System.out.println("同一个实例");
        } else {
            System.out.println("不同实例");

        }

    }

}
